﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExtEasy.Base;

namespace ExtEasy.Controls
{
    public class ExtListItem
    {
        public ExtListItem()
        {
            ToolTip = string.Empty;
        }
        public string ID { get; set; }
        public string Text { get; set; }
        public object Item { get; set; }
        public bool IsSelected { get; set; }
        public string Style { get; set; }
        public string ToolTip { get; set; }
    }

    public class ExtListGroup
    {
        public string ID { get; set; }
        public string Text { get; set; }
        public string Style { get; set; }
        public List<ExtListItem> Items { get; set; }
        public ExtListGroup()
        {
            Items = new List<ExtListItem>();
        }

    }

    public class ExtDropDownList : ExtControl
    {
        private bool _Checked = false;
        public bool Checked
        {
            get { return _Checked; }
            set { HTMLChanged = true; _Checked = value; }
        }

        #region Members
        public string Class { get; set; }
        // public List<ExtListItem> Items { get; set; }
        public List<ExtListGroup> Groups { get; set; }
        private string _SelectedID = "";
        public string Style = "";
        public string SelectedID
        {
            get { return _SelectedID; }
            set { _SelectedID = value; HTMLChanged = true; }
        }

        public void SetSelectedIDs(string s)
        {
            HTMLChanged = true;
            if (string.IsNullOrEmpty(s)) { SelectedIDs = new List<string>(); return; }
            string[] split = s.Split(',');
            SelectedIDs = new List<string>();
            for (int i = 0; i < split.Length; i++)
                SelectedIDs.Add(split[i]);
        }

        public bool IsIDSelected(string id)
        {
            return SelectedIDs.Contains(id);
        }


        public List<string> SelectedIDs { get; set; }

        public string Width { get; set; }
        public string Height { get; set; }

        public string ClientID
        {
            get { return ID + "_Select1"; }
        }

        public bool IsMultiple { get; set; }

        private bool _ShowGroups = false;
        public bool ShowGroups
        {
            set
            {
                _ShowGroups = value;
            }
            get
            {
                return _ShowGroups;
            }

        }

        #endregion

        #region Methods
        public ExtListItem GetItemByID(string id)
        {
            for (int i = 0; i < Groups.Count; i++)
            {
                for (int j = 0; j < Groups[i].Items.Count; j++)
                {
                    if (Groups[i].Items[j].ID == id) return Groups[i].Items[j];
                }
            }
            return null;
        }

        public ExtListItem SelectedItem
        {
            get
            {
                return GetItemByID(_SelectedID);
            }
        }
        #endregion


        public ExtDropDownList(string id, string script)
            : base(id)
        {
            Class = string.Empty;
            ID = id;
            StartTag = string.Empty;
            EndTag = string.Empty;
            _ChangeScript = script;
            Groups = new List<ExtListGroup>();
            IsMultiple = false;
            SelectedIDs = new List<string>();
            Width = "";
            Height = "";
        }

        public override ExtAjaxRes ProcessOperation(System.Collections.Specialized.NameValueCollection c)
        {
            throw new NotImplementedException();
        }

        //protected override string RenderControl()
        //{
        //    StringBuilder stb = new StringBuilder();
        //    stb.AppendFormat("<select id=\"{0}_Select1\" onchange=\"GetResult('{1}',this.options[this.selectedIndex].value)\" style=\"font-family:arial; font-size:11px;{2}\" >",ID,_ChangeScript,Style);
        //    for (int i = 0; i < Groups.Count; i++)
        //    {
        //        stb.AppendFormat("<optgroup id=\"{0}\" label=\"{1}\" style=\"{2}\">",Groups[i].ID,Groups[i].Text,Groups[i].Style);
        //        for (int j = 0; j < Groups[i].Items.Count; j++)
        //        {
        //            stb.AppendFormat("<option id=\"{0}\" value=\"{0}\"  style=\"{1}\" {2}>{3}", Groups[i].Items[j].ID, Groups[i].Items[j].Style, Groups[i].Items[j].ID == _SelectedID ? " selected=\"selected\" " : "", Groups[i].Items[j].Text);
        //            stb.Append("</option>");
        //        }
        //        stb.Append("</optgroup>");
        //    }
        //    stb.Append("</select>");

        //    return stb.ToString();
        //}
    }
}
